package edu.lnu.project.timeeditapp.parser;
/**
 * 
 */

import java.io.File;
import java.util.ArrayList;
import android.os.Environment;

/**
 * @author Libra
 *
 */
public class ImportFiles {
	
	private static ArrayList<File> files = new ArrayList<File>();
	private final static String pattern = ".xls";

	/**
	 * Scan files on the sd card
	 * 
	 * getOurXlsFiles(Environment.getExternalStorageDirectory()); <-- This command search the whole SD card
	 * 
	 * @param dir
	 */
	private static ArrayList<File> getFile(File dir) {
		
		// Check so that the sd card is available
		if(isStoreageAvailable()){	
			// Create a list with all the dir
			File listFile[] = dir.listFiles(); 
			
			if (listFile != null) {
			    for (int i = 0; i < listFile.length; i++) {
			    	// Check if the 'file' is a directory
			        if (listFile[i].isDirectory()) { 	
			        	// Go inside the found directory
			        	getFile(listFile[i]);		
			        } 
			        else {
			        	if(listFile[i].getName().endsWith(pattern)){
			        		files.add(listFile[i]);
			        	}
			        }
			    }
			}
		}
		else{
			System.out.println("Error SD card not mounted");
			return null;
		}
		return files;
	}
	
	/**
	 * Fetching all files found during scan
	 * 
	 * @return ArrayList<File>
	 */
	public static ArrayList<File> getFiles() {
		
		// Environment.getExternalStorageDirectory() <-- Select all files 
		return getFile(Environment.getExternalStorageDirectory());
	}
	
	/**
	 * Check state of media storage.
	 * 
	 * @return true if mounted
	 */
	private static boolean isStoreageAvailable() {
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}

}
